/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bhims;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.LogManager;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Rein
 */
public class CommonFunctions {

    WebClient initialiazeWebClient() {
        WebClient wc = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
        wc.getOptions().setCssEnabled(false);
        wc.getOptions().setThrowExceptionOnScriptError(false);
        wc.getOptions().setJavaScriptEnabled(true);
        wc.getOptions().setUseInsecureSSL(true);
        LogManager.getLogManager().reset();
        return wc;
    }

    void LoadTextToBlockingQueue(String FilePath, ArrayBlockingQueue AccountsBlockingQueue) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(FilePath)));
            String line;
            while ((line = br.readLine()) != null) {
                AccountsBlockingQueue.add(line);
            }
            br.close();
        } catch (Exception e) {
        }
    }

    void loadStringArraytoBlockingQueue(String[] TextStrings, ArrayBlockingQueue KeywordsBlockingQueue) {
        for (int i = 0; i < TextStrings.length; i++) {
            String string = TextStrings[i];
            KeywordsBlockingQueue.add(string);
        }
    }

    void LoadTextFileTojTextArea(JTextArea jTextArea1Users) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));


        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String Filename = fc.getSelectedFile().getAbsolutePath();
            try {
                try (BufferedReader br = new BufferedReader(new FileReader(Filename))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        jTextArea1Users.append(line + "\n");
                    }
                }
                jTextArea1Users.setText(jTextArea1Users.getText().trim());

            } catch (Exception ex) {
            }
        }
    }

    void LoadTextFilePathTojTextField(JTextField jTextField1Accounts) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));


        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            jTextField1Accounts.setText(fc.getSelectedFile().getAbsolutePath());            
        }
    }
}
